package md.frolov.bible.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import md.frolov.bible.Constants;

import org.eclipse.core.runtime.Platform;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class CrossReferenceProvider {
	private static CrossReferenceProvider instance;
	private static final String refPath = "/books/references.refs";
	private Map<String,List<String>> refMap = new HashMap<String,List<String>>();
	
	public static CrossReferenceProvider getInstance(){
		if(instance==null) instance = new CrossReferenceProvider(); 
		return instance;
	}
	
	private CrossReferenceProvider(){
		init();
	}
	
	private void init(){
		File workspace = Platform.getLocation().toFile();
		File file = new File(workspace.getAbsolutePath()+refPath);
		
		try{
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			System.out.println("Parsing ref file: "+file.getName());
			long time1 = System.currentTimeMillis();
			sp.parse(new InputSource(new InputStreamReader(new FileInputStream(file),"UTF-8")), new XMLHandler());
			System.out.println("Parsed in "+(System.currentTimeMillis()-time1)/1000.0+" ms");
		} catch (FileNotFoundException e){
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private class XMLHandler extends DefaultHandler{
		List<String> lastRefList;
		
		@Override
		public void endElement(String uri, String localName, String name)
				throws SAXException {
			// TODO Auto-generated method stub
			super.endElement(uri, localName, name);
		}

		@Override
		public void startElement(String uri, String localName, String name,	Attributes attributes) throws SAXException {
			if(name.equals(Constants.XML_TAG_REFERENCES_VERSE)){
				lastRefList = new ArrayList<String>();
				refMap.put(attributes.getValue(Constants.XML_ATTRIBUTES_REFERENCES_ID), lastRefList);
				return;
			}
			
			if(name.equals(Constants.XML_TAG_REFERENCES_REFERENCE)){
				StringBuffer sb = new StringBuffer(attributes.getValue(Constants.XML_ATTRIBUTES_REFERENCES_AID));
				if(attributes.getValue(Constants.XML_ATTRIBUTES_REFERENCES_ZID)!=null)
					sb.append("-").append(attributes.getValue(Constants.XML_ATTRIBUTES_REFERENCES_ZID));
				lastRefList.add(sb.toString());
				return;
			}
		}
		
	}
	
	public List<String> getReferences(String id){
		return refMap.get(id);
	}
	
}
